<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      width: 400px;
      height: 400px;
      background-repeat: no-repeat;
      background-size: contain;
    }
  </style>
</head>

<body>
  <input type="file" class="file">
  <br>
  <button class="btn">上传文件</button>
  <div class="box"></div>
  <script>
    const input = document.querySelector(".file");
    const box = document.querySelector(".box");

    input.addEventListener("change", async function () {
      const file = this.files[0];
      console.log(file);
      const formData = new FormData();
      formData.append("file", file);

      const res = await fetch("http://localhost:3000/upload", {
        method: "POST",
        body: formData
      });

      const result = await res.json();
      console.log(result);
      box.style.backgroundImage = `url(${result.file})`;
    })
  </script>
</body>

</html>